// pages/joke/joke.js

import uitlJoke from "../../utils/util_joke"
// 连接数据库（放在Page外面，作为页面的全局变量）
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    tabItemsArray: [{
      type: "video",
      name: "视频"
    }, {
      type: "image",
      name: "图片"
    }, {
      type: "text",
      name: "文本"
    }],
    videoJokeArray: [],
    imageJokeArray: [],
    textJokeArray: [],

    page: 1
  },
  toJokeDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/joke/jokeDetail/jokeDetail?id=${id}`,
    })
  },
  toCommentList: function (e) {
    let {
      id
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/comment/comment?id=${id}`,
    })
  },
  changeTabIndex: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    })
    this.changeJokes(e.currentTarget.dataset.type)
  },
  changeJokes: function (type) {
    var result;
    var that = this;
    result = wx.cloud.callFunction({
      // 云函数名称
      name: 'getJokeVotes',
      // 传给云函数的参数

    }).then(res => {
      console.log(res);
      result = res.result;

      uitlJoke.getJokeList(type, this.data.page, function (res) {
        console.log(res.items);
        if (type == "video") {
          for (let i = 0; i < res.items.length; i++) {
            if (result.data) {
              for (let j = 0; j < result.data.length; j++) {
                if (res.items[i].id == result.data[j].id) {
                  res.items[i].votes.up += result.data[j].votes.up;
                  res.items[i].votes.down -= result.data[j].votes.down;
                }
              }
            }

            if (res.items[i].pic_size[0] > res.items[i].pic_size[1]) {
              res.items[i].width = 710;
              res.items[i].height = res.items[i].pic_size[1] / res.items[i].pic_size[0] * 710
            } else {
              res.items[i].height = 710;
              res.items[i].width = res.items[i].pic_size[0] / res.items[i].pic_size[1] * 710
            }
          }


          that.setData({
            videoJokeArray: res.items
          })
        }
        if (type == "image") {

          for (var i = 0; i < res.items.length; i++) {

            if (res.items[i].format == "multi") {
              if (res.items[i].attachments.length == 1) {
                res.items[i].imageClass = "c1";
              } else if (res.items[i].attachments.length == 2 || res.items[i].attachments.length == 4) {
                res.items[i].imageClass = "c2";
              } else {
                res.items[i].imageClass = "c3";
              }
            }
          }


          that.setData({
            imageJokeArray: res.items
          })


          console.log(that.data.imageJokeArray);
        }
        if (type == "text") {
          that.setData({
            textJokeArray: res.items
          })
        }

      })

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.changeJokes(this.data.tabItemsArray[this.data.currentTab].type)
  },

  addVoteById: function (type, id, upordown) {
    if (type == "video") {
      for (var i = 0; i < this.data.videoJokeArray.length; i++) {
        if (this.data.videoJokeArray[i].id == id) {
          if (upordown) {
            this.setData({
              [`videoJokeArray[${i}].votes.up`]: this.data.videoJokeArray[i].votes.up + 1
            })
          } else {
            this.setData({
              [`videoJokeArray[${i}].votes.down`]: this.data.videoJokeArray[i].votes.down - 1
            })
          }
        }
      }
    }
  },

  
  onVotesTap(e) {

    console.log(e.currentTarget.dataset.id);
    var votes = e.currentTarget.dataset.upordown ? {
      "up": 1,
      "down": 0
    } : {
      "up": 0,
      "down": 1
    };
    console.log(votes);
    wx.cloud.callFunction({
      // 云函数名称
      name: 'addJokeVotes',
      // 传给云函数的参数
      data: {
        id: e.currentTarget.dataset.id,
        votes: votes
      },
    }).then(res => {
      console.log(res);
    })
    this.addVoteById("video", e.currentTarget.dataset.id, e.currentTarget.dataset.upordown, 1);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})